import 'babel-polyfill'
import Vue from 'vue'
import App from "./App.vue";
import VueRouter from 'vue-router'
import VueResource from 'vue-resource'
import VueImg from 'v-img'
import $ from 'jquery'
import BootstrapJs from './assets/js/bootstrap.min.js'
import Datetimepicker from './assets/js/bootstrap-datetimepicker.js'
import routes from './router/routes.js'
import Config from './config.js'
import iView from 'iview'
import ElementUI from 'element-ui'
import './theme/iview-theme.less'
import dataTable from './constants/tableConfig.js'
import VueClipboard from 'vue-clipboard2'
import config from "./config";
import Utils from './assets/js/utils.js'
//开启debug模式
Vue.use(VueRouter)
Vue.use(VueResource)
Vue.use(VueImg)
Vue.prototype.utilHelper = Config
Vue.prototype.utils = Utils
Vue.use(iView)
Vue.use(iView, {
  transfer: true,
  size: 'large'
})
Vue.use(dataTable)
Vue.use(VueClipboard)
Vue.config.debug = true
Vue.config.devtools = true
Vue.prototype.utilHelper = Config
VueClipboard.config.autoSetContainer = true
Vue.use(ElementUI)

const router = new VueRouter({
  //mode: 'history',
  base: __dirname,
  routes
})
router.beforeEach(({ meta, path }, from, next) => {
  var userAgent = navigator.userAgent
  if (userAgent.indexOf('MicroMessenger') > -1) {
    if (path !== '/disableWechat') {
      return next({
        path: '/disableWechat'
      })
    } else {
      next()
    }
  }

  if ('/resetPassword' == path || '/forgetPassword' == path) {
    return next()
  }
  //当返回到顶层时,将当前路径设置为下一个返回路径
  if ('/index' == path || '/' == 'path' || '/app' == path) {
    //当返回至 /app 时,往前走一步,跳转到首页
    if ('/app' == path) {
      window.history.forward(1)
    }
    history.pushState(null, null, document.URL)
  }

  var isLogin = window.localStorage['4_isLogin']
  if ((!isLogin || isLogin == 'null') && path !== '/login') {
    return next({
      path: '/login'
    })
  }
  next()
})


let exportAgencyUrlPrefix = 'exportAgency';
Vue.http.interceptors.push((request, next) => {
  //绑定登陆ID
  let companyUuid = getQueryString('companyUuid')
  var userId = window.localStorage.getItem('4_userId')
  var requestBody = request.getBody()
  if(!request.url.includes('://')) {
    request.url = Config.apiUrl + request.url
  }
  if (requestBody && requestBody.params && userId) {
    if (
      request.url.indexOf('getLogo') == -1 &&
      request.url.indexOf('login') == -1
    ) {
      requestBody.params.userId = userId
    }
    if (request.url.indexOf('loginOut') != -1) {
      requestBody.params.userId = userId
    }
  }
  if (requestBody && requestBody.params) {
    requestBody.params.loginType = '4'
  } else {
    var params = {
      params: {
        loginType: '4'
      }
    }
    if (requestBody) {
      requestBody.params = params
    }
  }
  request.body = requestBody
  request.headers.set('userId', userId)
  request.headers.set('companyUuid', companyUuid)
  request.headers.set('loginType', '4');

  next(response => {
    var isLogin = window.localStorage['4_isLogin']
    if (!response.ok && isLogin == 'true') {
      router.replace({
        path: '/login'
      })
      window.localStorage.setItem('4_isLogin', null)
      return
    }
    //验证登陆
    if (isLogin == 'true' && response.url.indexOf(exportAgencyUrlPrefix) < 0) {
      if (typeof response.body == 'string') {
        var responseBody = JSON.parse(response.body)
        var result = responseBody.result
        if (result && result == '-1') {
          router.replace({
            path: '/login'
          })
          window.localStorage.setItem('4_isLogin', null)
          return
        }
      } else if (typeof response.body == 'object') {
        var result = response.body.result
        if (result && result == '-1') {
          router.replace({
            path: '/login'
          })
          return
        }
      }
    }
  })
})

new Vue({
	el: "#app",
	router,
	render: r => r(App)
})

function getQueryString(name) {
  var reg = new RegExp('(^|&)' + name + '=([^&]*)(&|$)', 'i')
  var r = window.location.search.substr(1).match(reg)
  if (r != null) {
    return unescape(r[2])
  }
  return null
}
